﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;

namespace mapex.api
{
    /// <summary>
    /// APIHandler 的摘要说明
    /// </summary>
    public class APIHandler : IHttpHandler
    {
        HttpContext context = null;
        public void ProcessRequest(HttpContext context)
        {
            string methodName = context.Request["method"];
            Type type = this.GetType();
            MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);

            context.Response.ContentType = "text/plain";

            MethodInfo method = methodInfos.FirstOrDefault(m => m.Name.ToLower().Equals(methodName.ToLower()));
            method.Invoke(this, new object[] { context });
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void SaveAPIContent(HttpContext context)
        {
            string content = context.Request["Content"];
            string control = context.Request["Control"];
            File.WriteAllText("D:/" + control + ".txt", content);
            context.Response.Write("");
        }

        public void GetAPIContent(HttpContext context)
        {
            string content = string.Empty;
            string control = context.Request["Control"];
            try
            {
                content = File.ReadAllText("D:/" + control + ".txt");
            }
            catch
            {
                content = string.Empty;
            }
            context.Response.Write(content);
        }
    }
}